<template>
  <el-dialog 
    class="edit-dialog"
    :visible.sync="isVisible"
    :close-on-click-modal="false"
    :title="title"
    width="600px">
    <el-form
      ref="editForm"
      :model="editForm"
      :rules="rules"
      autocomplete="off"
      label-position="right" 
      label-width="125px"
    >
      <el-form-item prop="code" label="申请人名称">
        <el-input
          ref="code"
          v-model="editForm.applicantName"
          placeholder="请输入申请人名称"
          name="code"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="name" label="申请人身份证号">
        <el-input
          ref="name"
          v-model="editForm.applicantIdCard"
          placeholder="请输入申请人身份证号"
          name="name"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="name" label="申请人手机号">
        <el-input
          ref="name"
          v-model="editForm.applicantPhone"
          placeholder="请输入申请人手机号"
          name="name"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="name" label="法人身份证号">
        <el-input
          ref="name"
          v-model="editForm.legalIdCard"
          placeholder="请输入法人身份证号"
          name="name"
          type="text"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="medium" @click="isVisible=false">取 消</el-button>
      <el-button size="medium" type="primary" :loading="submitLoading" @click="editSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {insertRole, getCsInfoHash, updateCsInfo} from '@/http/apiUrls.js';
import { GZCA } from '@/utils/gzca.js';
import {getFormatDate} from '@/utils/date.js';
export default {
  name: 'EditForm',
  props: {
    // 弹窗显示
    dialogVisible: { type: Boolean, default: false},
    // 弹窗类型
    type: { type: String, default: "add" }
  },
  computed: {
    // 控制显示
    isVisible: {
      get() { 
        return this.dialogVisible; 
      },
      set() {
        this.$emit('close')
        this.$refs.editForm.resetFields();
        this.editForm = this.initEditObj()
      }
    },
    // 标题
    title:{
      get(){
        return '修改场所码信息'
      }
    }
  },
  data() {
    return {
      submitLoading:false,
      editForm: this.initEditObj(),
      rules: {
        applicantIdCard: {
          required: true,
          message: '请输入申请人身份证号',
          trigger: "blur"
        },
        applicantName: {
          required: true,
          message: '请输入申请人名称',
          trigger: "blur"
        },
        applicantPhone: {
          required: true,
          message: '请输入申请人手机号',
          trigger: "blur"
        },
        legalIdCard: {
          required: true,
          message: '请输入法人身份证号',
          trigger: "blur"
        }
      },
    }
  },
  methods: {
    // 修改传递值
    setEditObj(val) {
      if(val){
        this.editForm = {
          id: val.id,
          applicantIdCard: val.applicantIdCard,
          applicantName: val.applicantName,
          applicantPhone: val.applicantPhone,
          legalIdCard: val.legalIdCard,
        };
      }
      
    },
    // 重置值
    initEditObj(){
      return {
        id: "",
        applicantIdCard: "",
        applicantName: "",
        applicantPhone: "",
        legalIdCard: "",
        serialNumber: '',
        signCert: '',
        signature: '',
        time: ''
      }
    },
    editSubmit(){
       this.$refs.editForm.validate(valid => {
        if (valid) {
          if(this.$SIGN_SWITCH == true){
            this.ukeyUpdate()
          }else{
            this.edit()
          }
        } else {
          return false;
        }
      });
    },
    insert(){
      // let requestData = this.editForm;
      // this.submitLoading = true;
      // this.$postJson(insertRole, requestData).then(res => {
      //   this.submitLoading = false;
      //   if(res){
      //     this.$message({
      //       type: 'success',
      //       message: '添加成功!'
      //     });
      //     this.isVisible = false;
      //     this.$emit('success')
      //   }
      // })
    },
    edit(){
      let requestData = this.editForm;
      this.submitLoading = true;
      this.$postJson(updateCsInfo, requestData).then(res => {
        this.submitLoading = false;
        if(res){
          this.$message({
            type: 'success',
            message: '修改成功!'
          });
          this.isVisible = false;
          this.$emit('success')
        }
      })
    },
    ukeyUpdate(){
      GZCA.init(res=>{
        if(!res){
          this.$message({
            type:"error",
            message:"Ukey连接失败",
          });
          return;
        }
        this.getUkeyList();
      });
    },
    // 获取证书列表
    getUkeyList(){
      const certType = 1;
      GZCA.GZCA_GetCertList(true, certType, res=>{
        if(!res.success){
          this.$message({
            type:"error",
            message:res.msg,
          });
          return;
        }
        this.getCert(res.ContainerName, certType);
      });
    },
    // 获取证书
    getCert(containerName, certType){
      GZCA.GZCA_ExportCert(containerName, certType, res=>{
        if(!res.success){
          this.$message({
            type:"error",
            message:res.msg,
          });
          return;
        }
        this.getSerial(res.CertB64, containerName);
      });
    },
    // 获取证书签名序列号
    getSerial(certB64,containerName){
      GZCA.GZCA_GetCertInfo(certB64, res=>{
        if(!res.success){
          this.$message({
            type:"error",
            message:res.msg,
          });
          return;
        }
        this.editForm.signCert = certB64
        this.editForm.serialNumber = res.CertSerial
        this.getCsHash(res.CertSerial, containerName);
      });
    },
    getCsHash(certSerial, containerName){
      const requestData = {
        id: this.editForm.id,
        serialNumber: certSerial
      }
      // 获取Hash
      this.$postJson(getCsInfoHash, requestData).then(res => {
        if(res){
          this.sign(containerName, res)
          // this.edit()
        }
      })
    },
    sign(containerName, data){
      GZCA.GZCA_Pkcs1SignData_PIN(containerName, data, (res) => {
        if (res.success) {
          const SignData = res.SignData;
          this.editForm.signature = SignData
          // this.editForm.time = getFormatDate(new Date(), 8);
          this.edit()
        } else {
          this.$message({
            type:"error",
            message:res.msg,
          });
        }
    });
    }
  }
}
</script>
<style lang="scss">
.edit-dialog{
  .el-dialog__header{
    padding: 15px 20px 15px;
    .el-dialog__title{
      color: #fff;
    }

    i{
      color: #fff;
    }
    
    background-color: #006ACC;
  }

  .el-dialog__footer{
    padding: 15px 20px 15px;
    border-top: 1px solid #dfe4ea;
  }
}
</style>